/**
 * Created by 小敏哥 on 2018/1/5.
 */
import commonAction from "../../../../../js/actions/commonAction";
import commonService from '../../../../../js/services/commonService';
import {Toast} from 'antd-mobile'

import {
    PECC_DETAIL, 
    ORDER_PAY_MOUNT
} from "./actionsTypes";

export const peccDetail = data => ({type: PECC_DETAIL, data: data});//获取车辆所选违章详情
export const orderPayAmount = data => ({type: ORDER_PAY_MOUNT, data: data}); //总计

export const getPeccDetail= (data, callBack) => {
    return commonAction.simplePost(data, 'emsc-partner/b/carViola/getPeccDetailById.ajax', peccDetail, callBack);
};

export const prepareUserOrder=(data,callBack) => {
    return commonAction.simplePost(data, 'emsc-partner/b/carViola/prepareOrder.ajax', orderPayAmount, callBack);
};

export const payMent=(data,isWXBrowser,callBack) => {
    return commonAction.simplePost(data, 'emsc-partner/b/carViola/addOrder.ajax', undefined, (resultData) => {
        if (isWXBrowser) {
            callBack&&callBack(resultData)
        } else {
            resultData['payType'] = data.payType
            pay(resultData,callBack)
        }
    });
};

export const pay=(data,callBack) => {
    let postData = {
        chargeOrderId: data.payOrderId,
		payType: data.payType
    },
    url = 'emsc-partner/b/o/pay';
    Toast.hide();
    Toast.loading("", 30, () => Toast.info("网络错误", 2));
    commonService.post(postData, url).then(resultData => {
        Toast.hide();
        if(resultData.code == 1000){
            if(data.payType == 3){
                sessionStorage.setItem('weixinPay',1)
                sessionStorage.setItem('forwardUrl',resultData.body.forwardUrl)
                sessionStorage.setItem('wxData',data.payOrderId)
            }
         callBack&&callBack(resultData.body.payUrl)
        }
        else{
            Toast.info(resultData.msg?resultData.msg:'网络错误', 2);
        }
    }).catch((e)=>{
        Toast.info("网络错误", 2);
        throw e;
    })
};

export const weixinQuery=(data,callBack) => {
    return commonAction.simpleGet(data, 'emsc-partner/b/o/qwxp', undefined, callBack);
};